package com.frontyard.chatty.connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayDeque;
import java.util.Deque;

import com.frontyard.chatty.connection.listener.ChattyServerListener;

public class ClientProcessor implements Runnable {

    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private Deque<String> messages = new ArrayDeque<String>();

    public ClientProcessor(Socket socket, ChattyServerListener listener) {
        this.socket = socket;
    }

    public void run() {

        try {
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // per protocol, first send and ACKN
            out.println("ACKN");

            // enter reading mode
            String msg;
            while ((msg = in.readLine()) != null) {
                processMessage(msg);
            }
        } catch (IOException e) {

        }
    }

    private void processMessage(String msg) {
        if (msg.length() > 0) {
            String type = msg.substring(0, 4);
            String data = msg.substring(4);
        }
    }

}
